// This example shows how subscribe to changes of multiple items and display each change, identifying the different // subscriptions by an integer. // // Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html . using System; using System.Threading; using OpcLabs.EasyOpc.DataAccess; using OpcLabs.EasyOpc.DataAccess.OperationModel; namespace DocExamples.DataAccess._EasyDAClient { partial class SubscribeMultipleItems { public static void StateAsInteger() { // Instantiate the client object. using (var client = new EasyDAClient()) { // Hook events client.ItemChanged += client_StateAsInteger_ItemChanged; Console.WriteLine("Subscribing..."); int[] handleArray = client.SubscribeMultipleItems(new[] { new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Random", 1000, state: 1), // An integer we have chosen to identify the subscription new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Trends.Ramp (1 min)", 1000, state: 2), // An integer we have chosen to identify the subscription new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Trends.Sine (1 min)", 1000, state: 3), // An integer we have chosen to identify the subscription new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Register_I4", 1000, state: 4) // An integer we have chosen to identify the subscription }); for (int i = 0; i < handleArray.Length; i++) Console.WriteLine($"handleArray[{i}]: {handleArray[i]}"); Console.WriteLine("Processing item changed events for 10 seconds..."); Thread.Sleep(10 * 1000); Console.WriteLine("Unsubscribing..."); } Console.WriteLine("Waiting for 5 seconds..."); Thread.Sleep(5 * 1000); Console.WriteLine("Finished."); } // Item changed event handler static void client_StateAsInteger_ItemChanged(object sender, EasyDAItemChangedEventArgs eventArgs) { // Obtain the integer state we have passed in. var stateAsInteger = (int)eventArgs.Arguments.State; // Display the data if (eventArgs.Succeeded) Console.WriteLine($"{stateAsInteger}: {eventArgs.Vtq}"); else Console.WriteLine($"{stateAsInteger} *** Failure: {eventArgs.ErrorMessageBrief}"); } } }
# This example shows how subscribe to changes of multiple items and display each change, identifying the different # subscriptions by an integer. # # Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html . # OPC client and subscriber examples in Python on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-Python . # The QuickOPC package is needed. Install it using "pip install opclabs_quickopc". import opclabs_quickopc import time # Import .NET namespaces. from OpcLabs.EasyOpc.DataAccess import * from OpcLabs.EasyOpc.DataAccess.OperationModel import * # Item changed event handler. def itemChanged(sender, eventArgs): # Obtain the integer state we have passed in. stateAsInteger = int(eventArgs.Arguments.State) if eventArgs.Succeeded: print(stateAsInteger, ': ', eventArgs.Vtq, sep='') else: print(stateAsInteger, ' *** Failure: ', eventArgs.ErrorMessageBrief, sep='') # Instantiate the client object. client = EasyDAClient() # Hook events. client.ItemChanged += itemChanged print('Subscribing item changes...') handleArray = IEasyDAClientExtension.SubscribeMultipleItems(client, [ DAItemGroupArguments('', 'OPCLabs.KitServer.2', 'Simulation.Random', 1000, 1), # an integer we have chosen to identify the subscription DAItemGroupArguments('', 'OPCLabs.KitServer.2', 'Trends.Ramp (1 min)', 1000, 2), # an integer we have chosen to identify the subscription DAItemGroupArguments('', 'OPCLabs.KitServer.2', 'Trends.Sine (1 min)', 1000, 3), # an integer we have chosen to identify the subscription DAItemGroupArguments('', 'OPCLabs.KitServer.2', 'Simulation.Register_I4', 1000, 4), # an integer we have chosen to identify the subscription ]) for i in range(len(handleArray)): print('handleArray[', i, ']: ', handleArray[i], sep='') print('Processing item change notifications for 1 minute...') time.sleep(60) print('Unsubscribing all items...') client.UnsubscribeAllItems() client.ItemChanged -= itemChanged print('Finished.')
' This example shows how subscribe to changes of multiple items and display each change, identifying the different ' subscriptions by an integer. ' ' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html . Imports System.Threading Imports OpcLabs.EasyOpc.DataAccess Imports OpcLabs.EasyOpc.DataAccess.OperationModel Namespace DataAccess._EasyDAClient Partial Friend Class SubscribeMultipleItems Public Shared Sub StateAsInteger() ' Instantiate the client object. Using client = New EasyDAClient() AddHandler client.ItemChanged, AddressOf client_StateAsInteger_ItemChanged Console.WriteLine("Subscribing...") Dim handleArray() As Integer = client.SubscribeMultipleItems(New DAItemGroupArguments() { New DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Random", 1000, state:=1), ' An integer we have chosen to identify the subscription New DAItemGroupArguments("", "OPCLabs.KitServer.2", "Trends.Ramp (1 min)", 1000, state:=2), ' An integer we have chosen to identify the subscription New DAItemGroupArguments("", "OPCLabs.KitServer.2", "Trends.Sine (1 min)", 1000, state:=3), ' An integer we have chosen to identify the subscription New DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Register_I4", 1000, state:=4) ' An integer we have chosen to identify the subscription }) For i As Integer = 0 To handleArray.Length - 1 Console.WriteLine($"handleArray[{i}]: {handleArray(i)}") Next i Console.WriteLine("Processing item changed events for 10 seconds...") Thread.Sleep(10 * 1000) Console.WriteLine("Unsubscribing...") End Using Console.WriteLine("Waiting for 5 seconds...") Thread.Sleep(5 * 1000) Console.WriteLine("Finished.") End Sub ' Item changed event handler Private Shared Sub client_StateAsInteger_ItemChanged(ByVal sender As Object, ByVal eventArgs As EasyDAItemChangedEventArgs) ' Obtain the integer state we have passed in. Dim stateAsInteger As Integer = CInt(eventArgs.Arguments.State) If eventArgs.Succeeded Then Console.WriteLine($"{stateAsInteger}: {eventArgs.Vtq}") Else Console.WriteLine($"{stateAsInteger} *** Failure: {eventArgs.ErrorMessageBrief}") End If End Sub End Class End Namespace
Copyright © 2004-2024 CODE Consulting and Development, s.r.o., Plzen. All rights reserved.
Send Documentation Feedback. Technical Support